package com.pzhu.servlet;

import com.pzhu.controller.BookController;
import com.pzhu.controller.impl.BookControllerImpl;
import com.pzhu.pojo.Book;
import com.pzhu.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FindBookServlet extends HttpServlet {
    private BookController bookController = new BookControllerImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session =req.getSession();
        if (session.getAttribute("login") == null) {
            req.getRequestDispatcher("/main").forward(req, resp);
            return;
        }
        List<Book> bookList;
        System.out.println(req.getParameter("id") );
        if (req.getParameter("id") == null) {
            bookList = bookController.selectAllBook();
        } else {
            Book book = bookController.selectBook(Long.valueOf(req.getParameter("id")));
            if (book==null){
                System.out.println(book);
                req.getRequestDispatcher("/main").forward(req, resp);
                return;
            }
            bookList = new ArrayList<Book>();
            bookList.add(book);
        }
        System.out.println(bookList.get(0).getId());
        req.setAttribute("bookList", bookList);
        req.getRequestDispatcher("/view/jsp/book/findBook.jsp").forward(req, resp);
        return;
    }
}
